package org.example.aop.proxy;

import org.springframework.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

public class Main {
    public static void main(String[] args) {
        //静态代理
        HouseSubject houseSubject = new RealHouseSubject();
        proxy proxy = new proxy(houseSubject);
        proxy.rentHouse();


        System.out.println("+++++++++++++++++++++++++++++++++++++++");

        /*
        *   动态代理
        *   public static Object newProxyInstance(ClassLoader loader,Class<?>[] interfaces,InvocationHandler h)
        *   loader:加载代理对象（需要代理的对象）
        *   interfaces: 被实现的接口
        *   h：代理要做的事情（继承InvocationHandler类的类）实现invoke方法
        *  */

//        动态代理接口 成功
/*        RealHouseSubject realHouseSubject = new RealHouseSubject();
        HouseSubject proxy1 = (HouseSubject) Proxy.newProxyInstance(
                realHouseSubject.getClass().getClassLoader(),
                new Class[]{HouseSubject.class},
                new JDKInvocationHandler(realHouseSubject)
        );
        proxy1.rentHouse();*/


//        //动态代理类  失败
//        RealHouseSubject realHouseSubject = new RealHouseSubject();
//        RealHouseSubject proxy1 = (RealHouseSubject) Proxy.newProxyInstance(
//                RealHouseSubject.class.getClassLoader(),
//                new Class[]{RealHouseSubject.class},
//                new JDKInvocationHandler(realHouseSubject)
//        );
//        proxy1.rentHouse();



        /*
        *动态代理
        *CGLibMethodInterceptor
        * */

        //动态代理类 成功
/*        RealHouseSubject realHouseSubject = new RealHouseSubject();
        RealHouseSubject realHouseSubject1  = (RealHouseSubject) Enhancer.create(realHouseSubject.getClass(), new CGLibMethodInterceptor(realHouseSubject));
        realHouseSubject1.rentHouse();
        //代理接口 成功
        HouseSubject houseSubject1 = (HouseSubject) Enhancer.create(realHouseSubject.getClass(),new CGLibMethodInterceptor(realHouseSubject));
        houseSubject1.rentHouse();*/

    }
}
